/**
 *  2013-5-15   上午10:57:41
 *  Created By niexiaoqiang
 */

package com.xiaoqiang.util;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Robot;

import org.apache.log4j.Logger;

/**
 * TODO Add Class Description
 */
public class MtwRobot {
	private static Logger log = Logger.getLogger(MtwRobot.class);
	private static Robot robot;
	private static MtwRobot instance = null;

	private MtwRobot() {
		try {
			robot = new Robot();
		} catch (AWTException e) {
			log.error("Robot init error");
		}
	}

	public static synchronized MtwRobot getInstance() {
		if (instance == null) {
			instance = new MtwRobot();
		}
		return instance;
	}

	public synchronized int pressKey(int keyCode) {
		log.debug("pressKey:  " + keyCode);
		robot.keyPress(keyCode);
		return 0;

	}

	public synchronized int releaseKey(int keyCode) {
		log.debug("releaseKey:  " + keyCode);
		robot.keyRelease(keyCode);
		return 0;

	}

	public synchronized int mouseMove(int x, int y) {
		log.debug("mouseMove x=" + x + "     y=" + y);
		PointerInfo p = MouseInfo.getPointerInfo();
		robot.mouseMove(p.getLocation().x + x, p.getLocation().y + y);
		return 0;
	}

	public synchronized int mouseWheel(int mouseWhileAMT) {
		log.debug("mouseWheel =" + mouseWhileAMT);
		robot.mouseWheel(mouseWhileAMT);
		return 0;
	}

	public synchronized int mousePress(int mouseButton) {
		log.info("mousePress =" + mouseButton);
		robot.mousePress(mouseButton);
		return 0;
	}

	public synchronized int mouseRelease(int mouseButton) {
		log.debug("mouseRelease =" + mouseButton);
		robot.mouseRelease(mouseButton);
		return 0;
	}
}
